using HCore.IOC.Beans.Factory.Attribute.Attr;
using HCore.IOC.Context.Event.Attr;
using HCore.IOC.Context.Stereotype;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.Support.CommandQuery;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.System.GunSystem;

namespace HCore.Tests.Runtime.ShootingEditor2D.Scripts.Command
{
    /// <summary>
    ///     给所有枪，增加一个弹夹量的子弹
    /// </summary>
    public class AddBulletCommand : ICommand
    {
    }

    /// <summary>
    ///     处理增加子弹命令的处理器
    /// </summary>
    [Component]
    public class AddBulletCommandHandler
    {
        [Autowired] private IGunSystem mGunSystem;

        /// <summary>
        ///     执行增加子弹命令
        /// </summary>
        /// <param name="command">增加子弹命令实例</param>
        [EventListener]
        private void ExecuteCommand(AddBulletCommand command)
        {
            // 增加当前枪一个弹夹量的子弹，
            AddBullet(mGunSystem.CurrentGun);

            // 给后备列队中的枪增加一个弹夹量的子弹
            foreach (var gunSystemGunInfo in mGunSystem.GunInfos) AddBullet(gunSystemGunInfo);
        }

        /// <summary>
        ///     为指定枪支增加一个弹夹量的子弹
        /// </summary>
        /// <param name="gunSystemGunInfo">枪支信息</param>
        private void AddBullet(GunInfo gunSystemGunInfo)
        {
            var gunConfigItem = mGunSystem.GetGunConfigByName(gunSystemGunInfo.Name.Value);
            // 手枪是无限后备子弹的，无需添加子弹
            if (gunConfigItem.NeedBullet)
                gunSystemGunInfo.BulletCountOutGun.Value += gunConfigItem.BulletMaxCount;
        }
    }
}